package lzyc.pointsManagement.business.service.impl;

import lombok.extern.slf4j.Slf4j;
import lzyc.pointsManagement.business.activity.convertor.ActivityApplyConvertor;
import lzyc.pointsManagement.business.activity.domain.ActivityApply;
import lzyc.pointsManagement.business.activity.domain.ActivityInfo;
import lzyc.pointsManagement.business.activity.dto.ActivityApplyDto;
import lzyc.pointsManagement.business.activity.dto.ActivityInfoDto;
import lzyc.pointsManagement.business.activity.mapper.ActivityApplyMapper;
import lzyc.pointsManagement.business.activity.mapper.ActivityInfoMapper;
import lzyc.pointsManagement.business.approval.convertor.ApprovalInfoConvertor;
import lzyc.pointsManagement.business.approval.domain.ApprovalInfo;
import lzyc.pointsManagement.business.approval.dto.ApprovalInfoDto;
import lzyc.pointsManagement.business.approval.service.ApprovalInfoService;
import lzyc.pointsManagement.business.attachment.convertor.AttachmentsConvertor;
import lzyc.pointsManagement.business.attachment.domain.Attachments;
import lzyc.pointsManagement.business.attachment.dto.AttachmentsDto;
import lzyc.pointsManagement.business.attachment.mapper.AttachmentsMapper;
import lzyc.pointsManagement.business.config.ApprovalStatus;
import lzyc.pointsManagement.business.config.BizType;
import lzyc.pointsManagement.business.points.domain.PointsInfo;
import lzyc.pointsManagement.business.points.service.PointsInfoService;
import lzyc.pointsManagement.business.service.ActivityApplyService;
import lzyc.pointsManagement.business.service.ActivityInfoService;
import lzyc.pointsManagement.business.service.AttachmentsService;
import lzyc.pointsManagement.common.core.domain.entity.SysUser;
import lzyc.pointsManagement.common.core.page.PageDomain;
import lzyc.pointsManagement.common.response.ResponseMessage;
import lzyc.pointsManagement.common.response.ResponseMessageUtility;
import lzyc.pointsManagement.common.utils.ShiroUtils;
import lzyc.pointsManagement.common.utils.uuid.IdUtils;
import lzyc.pointsManagement.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author liuyuan
* @description 针对表【activity_apply】的数据库操作Service实现
* @createDate 2024-03-10 20:16:30
*/
@Service
@Slf4j
public class ActivityApplyServiceImpl implements ActivityApplyService {

    ActivityApplyMapper activityApplyMapper;
    AttachmentsMapper attachmentsMapper;

    @Autowired
    AttachmentsService attachmentsService;

    ISysUserService iSysUserService;

    ApprovalInfoService approvalInfoService;

    PointsInfoService pointsInfoService;

    ActivityInfoMapper activityInfoMapper;



    public ActivityApplyServiceImpl(ActivityApplyMapper activityApplyMapper,
                                    AttachmentsMapper attachmentsMapper,
                                    ISysUserService iSysUserService,
                                    ApprovalInfoService approvalInfoService,
                                    PointsInfoService pointsInfoService,
                                    ActivityInfoMapper activityInfoMapper) {
        this.activityApplyMapper = activityApplyMapper;
        this.attachmentsMapper = attachmentsMapper;
        this.iSysUserService = iSysUserService;
        this.approvalInfoService = approvalInfoService;
        this.pointsInfoService = pointsInfoService;
        this.activityInfoMapper = activityInfoMapper;
    }


    @Override
    public ResponseMessage addActivityApply(ActivityApplyDto activityApplyDto) {
        ResponseMessage result;
        try {
            ActivityApply entity = ActivityApplyConvertor.convertToEntity(activityApplyDto);
            activityApplyMapper.addActivityApply(entity);
            ApprovalInfo approvalInfo = new ApprovalInfo();
            approvalInfo.setApprovalId(IdUtils.randomUUID());
            approvalInfo.setProjectId(activityApplyDto.getId());
            approvalInfo.setType(BizType.Activity.getCode());
            approvalInfo.setStatus(ApprovalStatus.PENDING.getCode());
            if (activityApplyDto.getUserid() == null) {
                approvalInfo.setApplyUserId(ShiroUtils.getLoginName());
            } else {
                approvalInfo.setApplyUserId(activityApplyDto.getUserid());
            }
            approvalInfoService.insert(approvalInfo);
            List<String> attachmentsIdList = activityApplyDto.getAttachmentIds();
            if (attachmentsIdList != null && !attachmentsIdList.isEmpty()) {
                attachmentsService.batchUpdateBizIds(attachmentsIdList, activityApplyDto.getId());
            }
            PointsInfo pointsInfo = new PointsInfo();
            pointsInfo.setPointsId(IdUtils.randomUUID());
            pointsInfo.setPointsDate(new Date());
            pointsInfo.setScore(0);
            pointsInfo.setType(approvalInfo.getType());
            pointsInfo.setPointsUserId(approvalInfo.getApplyUserId());
            pointsInfo.setProjectId(entity.getActivityId());
            if (!pointsInfoService.checkPointExist(pointsInfo.getProjectId(), pointsInfo.getPointsUserId())) {
                pointsInfoService.insert(pointsInfo);
            }
            result = ResponseMessageUtility.createSuccess();
        } catch (RuntimeException e) {
            result = ResponseMessageUtility.createError("申请失败。" + e.getMessage());
        }
        return result;
    }

    @Override
    public ResponseMessage getActivityApplyList(PageDomain pageInfo) {
        ResponseMessage result = ResponseMessageUtility.createSuccess();
        int offset = (pageInfo.getPageNum() - 1) * pageInfo.getPageSize();
        int size = pageInfo.getPageSize();
        List<ActivityApply> activityApplies = activityApplyMapper.getActivityApplyListByPage(offset, size);
        if (activityApplies != null && !activityApplies.isEmpty()) {
            List<ActivityApplyDto> dtoList = activityApplies.stream().map(ActivityApplyConvertor::convertToDto).collect(Collectors.toList());
            result.setData(dtoList);
            result.setPageInfo(pageInfo);
        }
        return result;
    }

    @Override
    public ResponseMessage getActivityApplyById(String id) {
        ResponseMessage result;
        ActivityApply activityApply = activityApplyMapper.getActivityApplyById(id);
        if (activityApply != null) {
            result = ResponseMessageUtility.createSuccess();
            List<Attachments> attachments = attachmentsMapper.selectAttachmentByBizId(activityApply.getId());
            ActivityApplyDto dto = ActivityApplyConvertor.convertToDto(activityApply);
            if (attachments != null && !attachments.isEmpty()) {
                dto.setAttachments(attachments.stream().map(AttachmentsConvertor::convertToDto).collect(Collectors.toList()));
            }
            ApprovalInfoDto approvalInfoDto = ApprovalInfoConvertor.convertToDto(approvalInfoService.queryByProjectId(activityApply.getId()));
            dto.setApprovalInfo(approvalInfoDto);
            result.setData(dto);
        } else {
            result = ResponseMessageUtility.createError("当前审批不存在。");
        }
        return result;
    }

    @Override
    public ResponseMessage getJoinActivityUser(String activityId) {
        ResponseMessage result = ResponseMessageUtility.createSuccess();
        List<ActivityApply> activityApplies = activityApplyMapper.getActivityApplyByActivityId(activityId);
        List<String> userIdList = activityApplies.stream().map(ActivityApply::getUserid).collect(Collectors.toList());
//        List<SysUser> users = iSysUserService.selectUserByIds(userIdList);
//        result.setData(users);
        return result;
    }

    @Override
    public ResponseMessage getUnJoinActivityUser(String activityId) {
        ResponseMessage result = ResponseMessageUtility.createSuccess();
        List<ActivityApply> activityApplies = activityApplyMapper.getActivityApplyByActivityId(activityId);
        List<String> userIdList = activityApplies.stream().map(ActivityApply::getUserid).collect(Collectors.toList());
//        List<SysUser> users = iSysUserService.selectUserNotInIds(userIdList);
//        result.setData(users);
        return result;
    }

    @Override
    public ResponseMessage getActivityApplyByActivityId(String activityId, PageDomain pageInfo) {
        ResponseMessage result = ResponseMessageUtility.createSuccess();
        List<ActivityApplyDto> activityApplyDtoList = new ArrayList<>();
        List<ActivityApply> activityApplies = activityApplyMapper.getActivityApplyByActivityId(activityId);
        for (ActivityApply activityApply : activityApplies) {
            ActivityApplyDto activityApplyDto = ActivityApplyConvertor.convertToDto(activityApply);
            List<Attachments> attachments = attachmentsMapper.selectAttachmentByBizId(activityApply.getId());
            if (attachments != null && !attachments.isEmpty()) {
                activityApplyDto.setAttachments(attachments.stream().map(AttachmentsConvertor::convertToDto).collect(Collectors.toList()));
            }
            ApprovalInfo approvalInfo = approvalInfoService.queryByProjectId(activityApply.getId());
            activityApplyDto.setApprovalInfo(ApprovalInfoConvertor.convertToDto(approvalInfo));
            activityApplyDtoList.add(activityApplyDto);
        }
        result.setData(activityApplyDtoList);
        return result;
    }
}




